package com.example.aopdemo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class OrderServiceTest {
    public static void main(String[] args) {
        // 使用注解配置方式加载Spring上下文
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        OrderService orderService = context.getBean(OrderService.class);

        System.out.println("========== 开始测试 OrderService AOP 功能 ==========");

        // 测试 add 方法
        testAddMethod(orderService);

        // 测试 delete 方法
        testDeleteMethod(orderService);

        // 测试 find 方法
        testFindMethod(orderService);

        System.out.println("========== 测试完成 ==========");
    }

    private static void testAddMethod(OrderService orderService) {
        System.out.println("\n--- 测试 add 方法 ---");
        try {
            orderService.add("iPhone 14", 2);
            System.out.println("add 方法执行成功");
        } catch (Exception e) {
            System.out.println("add 方法执行失败: " + e.getMessage());
        }
    }

    private static void testDeleteMethod(OrderService orderService) {
        System.out.println("\n--- 测试 delete 方法 ---");
        try {
            orderService.delete("iPhone 14");
            System.out.println("delete 方法执行成功");
        } catch (Exception e) {
            System.out.println("delete 方法执行失败: " + e.getMessage());
        }
    }

    private static void testFindMethod(OrderService orderService) {
        System.out.println("\n--- 测试 find 方法 ---");
        try {
            int count = orderService.find("iPhone 14");
            System.out.println("find 方法执行成功，返回结果: " + count);
        } catch (Exception e) {
            System.out.println("find 方法执行失败: " + e.getMessage());
        }
    }
}